package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.respository.StrategyRepository;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyRepository strategyRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        strategyRepository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        strategyRepository.save(strategy);
    }

    @Override
    public void delete(String id) {
        strategyRepository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return strategyRepository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return strategyRepository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {

        // MQL拼接语句
        Query query = new Query();

        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if (StringUtils.hasLength(qo.getThemeId())) {
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        // 这里只有是前台的情况下只查询能发布的
        if (StringUtils.hasLength(qo.getState())) {
            query.addCriteria(Criteria.where("state").is(1));
        }

        // 分页对象 传入分页需要的参数 当前页-1  每页行数  排序规则  排序依据
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        // 返回分页对象
        // Page 是接口
        // PageImpl 是实现类
        return DBHelper.query(template, Strategy.class, query, pageable);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {

        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
        StrategyTheme theme = strategyThemeService.get(strategy.getThemeId());

        // 目的地id
        strategy.setDestId(catalog.getDestId());
        // 目的地名字
        strategy.setDestName(catalog.getDestName());
        // 主题名
        strategy.setThemeName(theme.getName());
        // 分类名
        strategy.setCatalogName(catalog.getName());
        // 是否国外
        List<Destination> toasts = destinationService.queryToasts(catalog.getDestId());
        if (toasts != null && toasts.size() > 0) {
            Destination destination = toasts.get(0);

            strategy.setIsabroad("中国".equals(destination.getName()) ? Strategy.ABROAD_NO : Strategy.ABROAD_YES);
        }

        if (StringUtils.hasText(strategy.getId())) {
            // 先查询
            Strategy temp = this.get(strategy.getId());
            // 再替换
            strategy.setCreateTime(temp.getCreateTime());
            strategy.setViewnum(temp.getViewnum());
            strategy.setReplynum(temp.getReplynum());
            strategy.setThumbsupnum(temp.getThumbsupnum());
            strategy.setSharenum(temp.getSharenum());
            strategy.setFavornum(temp.getFavornum());
            // 后更新
            this.update(strategy);
        } else {
            // 创建时间
            strategy.setCreateTime(new Date());

            this.save(strategy);
        }

        // es数据同步修改  异步进行修改 使用消息中间件

    }

    @Override
    public List<Strategy> queryByCatalogId(String catalogId) {

        return strategyRepository.findByCatalogId(catalogId);
    }

    @Override
    public List<Strategy> queryViewnumTop3(String destId) {

        // 点击量排序
        // 前三 分页 1页 3个
        // 条件 目的地id
        Pageable pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");

        return strategyRepository.findByDestId(destId, pageable);
    }

    @Override
    public Strategy changeState(String id, int state) {
        // 先把攻略查出来
        Strategy strategy = this.get(id);
        // 修改状态
        strategy.setState(state);
        // 保存
        this.saveOrUpdate(strategy);
        return strategy;
    }

    @Override
    public void updateStatisVo(StrategyStatisVO vo) {
        Query query = new Query();

        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));

        Update update = new Update();
        update.set("viewnum", vo.getViewnum());
        update.set("favornum", vo.getFavornum());
        update.set("replynum", vo.getReplynum());
        update.set("thumbsupnum", vo.getThumbsupnum());
        update.set("sharenum", vo.getSharenum());

        template.updateMulti(query, update, Strategy.class);
    }

    @Override
    public List<Strategy> queryByDestId(String id) {
        return strategyRepository.queryByDestId(id);
    }
}


































































